package it.patamau.bukkit;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Properties;

import org.bukkit.ChatColor;
import org.bukkit.plugin.Plugin;

/**
 * Used to retrieve messages.
 * Can also be used to edit, remove or localize such messages
 * @author patamau
 */
public class PluginMessages {

	public static final String VERSION = "0.1";
	
	/**
	 * Replace %COLOR with the actual chat color code
	 * @param msg
	 * @return
	 */
	public static MessageFormat getFormat(String msg){
		String str = msg;
		str = str.replaceAll("\\%AQUA", ChatColor.AQUA.toString());
		str = str.replaceAll("\\%BLACK", ChatColor.BLACK.toString());
		str = str.replaceAll("\\%BLUE", ChatColor.BLUE.toString());
		str = str.replaceAll("\\%DARK_AQUA", ChatColor.DARK_AQUA.toString());
		str = str.replaceAll("\\%DARK_BLUE", ChatColor.DARK_BLUE.toString());
		str = str.replaceAll("\\%DARK_GRAY", ChatColor.DARK_GRAY.toString());
		str = str.replaceAll("\\%DARK_GREEN", ChatColor.DARK_GREEN.toString());
		str = str.replaceAll("\\%DARK_PURPLE", ChatColor.DARK_PURPLE.toString());
		str = str.replaceAll("\\%DARK_RED", ChatColor.DARK_RED.toString());
		str = str.replaceAll("\\%GOLD", ChatColor.GOLD.toString());
		str = str.replaceAll("\\%GRAY", ChatColor.GRAY.toString());	
		str = str.replaceAll("\\%GREEN", ChatColor.GREEN.toString());	
		str = str.replaceAll("\\%LIGHT_PURPLE", ChatColor.LIGHT_PURPLE.toString());	
		str = str.replaceAll("\\%RED", ChatColor.RED.toString());
		str = str.replaceAll("\\%WHITE", ChatColor.WHITE.toString());
		str = str.replaceAll("\\%YELLOW", ChatColor.YELLOW.toString());
		//fix message format arguments selection
		str = str.replaceAll("\\{", "{");
		str = str.replaceAll("\\}", "}");
		return new MessageFormat(str);
	}
	
	private Properties messages; //loaded strings
	private HashMap<String, MessageFormat> cache; //internal message cache
	private String filename; //reference to source file
	
	public PluginMessages(Plugin plugin){
		messages = new Properties();
		cache = new HashMap<String,MessageFormat>();
		String name = plugin.getDescription()!=null?plugin.getDescription().getName():null;
		if(name==null){
			name = plugin.getClass().getSimpleName();
		}
		filename = name+".messages";
	}
	
	public void load() throws IOException{
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(new File(filename));
			messages.load(fis);
			fis.close();
			//create message format cache for all the messages
			for(String k: messages.stringPropertyNames()){
				this.set(k, messages.getProperty(k));
			}
		} catch (FileNotFoundException e) {
			store();
		}
	}
	
	/**
	 * Save the current set of messages to file
	 * @throws IOException
	 */
	public void store() throws IOException{
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(new File(filename));
			messages.store(fos, this.getClass().getSimpleName()+" "+VERSION);
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Retrieve the message identified by the key argument
	 * If no such message is defined, null is returned
	 * @param key
	 * @param
	 * @return
	 */
	public String get(String key, Object ... args){
		MessageFormat f = cache.get(key);
		if(f==null){
			set(key,"");
			return "";
		}else{
			System.err.println("FORMAT PATTERN: "+f.toPattern());
			return f.format(args);
		}
	}
	
	/**
	 * Store a default value for the message
	 * @param key
	 * @param val
	 * @return
	 */
	public void set(String key, String val){
		if(val==null) throw new NullPointerException("Cannot set a null value for message '"+key+"'");
		System.err.println(key+" = "+val);
		messages.setProperty(key, val);
		cache.put(key, getFormat(val));
	}
	
	public int size(){
		return messages.size();
	}
}
